#pragma once
#include "Runtime/BattleFirePrefix.h"
class BattleFireEvent
{
public:
    std::mutex mMutex;
    std::condition_variable mConditionVariable;
    virtual void TriggerOne()
    {
        std::unique_lock<std::mutex> UniqueLock(mMutex);
        mConditionVariable.notify_one();
    }
    virtual void TriggerAll()
    {
        std::unique_lock<std::mutex> UniqueLock(mMutex);
        mConditionVariable.notify_all();
    }
    virtual bool Wait( BattleFireUInt32 WaitTime = (BattleFireUInt32)-1)
    {
        std::unique_lock<std::mutex> UniqueLock(mMutex);
        return std::cv_status::timeout != mConditionVariable.wait_for(UniqueLock,std::chrono::seconds(WaitTime));
    }
};
class FScopedEvent
{
public:
    FScopedEvent()
    {
        Event=new BattleFireEvent;
    }
    ~FScopedEvent()
    {
        if ( Event )
        {
            Event->Wait();
            delete Event;
            Event = nullptr;
        }
    }
    void Trigger()
    {
        Event->TriggerAll();
    }
    bool IsReady()
    {
        if ( Event )
        {
            if ( Event->Wait(1) )
            {
                delete Event;
                Event = nullptr;
                return true;
            }
            return false;
        }
        return true;
    }
    BattleFireEvent* Get()
    {
        return Event;
    }
private:
    BattleFireEvent* Event;
};